/**
 * 后端接口地址
 * @type {string}
 */
const BASE_URL = 'http://127.0.0.1:8002';

const AUTH_HEADER = "Authorization";

const LOGIN_PAGE = getRootPath() + '/login.html';


/**
 * 获取存储的token
 * @returns {string}
 */
function getToken() {
    return 'Bearer ' + layui.data('dimples').token;
}

/**
 * 获取存储的user
 * @returns {string}
 */
function getUser() {
    let user = layui.data('dimples').user;
    if (user instanceof Array) {
        return user[0];
    } else {
        return user;
    }
}

/**
 * 获取URL中的参数
 * @param href
 * @param field
 * @returns {string}
 */
function parseParamByHref(href, field) {
    let url = new URL(href)
    let params = new URLSearchParams(url.search.slice(1))
    return params.get(field);
}

/**
 * 存储数据到 layui.data('dimpels')
 * @param key
 * @param value
 */
function saveData(key, value) {
    layui.data('dimples', {
        key: key, value: value
    });
}

/**
 * 获取localStorage中的数据() - layui.data('dimpels')
 * @returns {*}
 */
function getAllData() {
    if (layui.data('dimples') == {} || layui.data('dimples') == [] || layui.data('dimples') == null || layui.data('dimples') == undefined) {
        location.href = getRootPath() + '/login.html';
    } else {
        return layui.data('dimples');
    }
}

/**
 * 获取随机UUID
 * @returns {string}
 * @constructor
 */
function UUID() {
    let s = [];
    let hexDigits = "0123456789abcdef";
    for (let i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);

    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
    return s.join("");

}

/**
 * js获取项目根路径
 * @returns {string} http://localhost:8083/app
 */
function getRootPath() {
    //获取当前网址，如： http://localhost:8083/app/share/menu.html
    let curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： /app/share/menu.html
    let pathName = window.document.location.pathname;
    let pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    let localhostPaht = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/app
    let projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return (localhostPaht + projectName);
}

/**
 * 检查是否存在某种权限
 * @param perm 权限标识
 * @returns {*} style="display: none;
 */
function checkPerms(perm) {
    let perms = getAllData().perms;
    return perms.includes(perm) ? '' : 'style="display: none;"' || perms.eq('*:*:*');
}

/**
 * 判断没有某种权限
 * @returns {*} style="display: none;
 */
function noPerms() {
    let len = arguments.length;
    let s = 0;
    let perms = getAllData().perms + ',*:*:*';
    for (let i = 0; i < len; i++) {
        if (perms.includes(arguments[i])) {
            s = s + 1;
        }
    }
    return s > 0 ? 'style="display: none;"' : '';
}